﻿using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
using System.Timers;

//From thread here : http://bytes.com/forum/thread657473.html
namespace WindowsFormsApplication1
{
    class WIFISensor : Sensor
    {
        const string wql_adaptor_list = "SELECT * FROM MSNdis_80211_BaseServiceSetIdentifier WHERE Active = True";
        const string wql_network_list = "SELECT * FROM MSNDis_80211_BSSIList";
        private Timer myTimer;
        private const int TIMER_INTERVAL = 40000;
        private List<string> wifi_list;

        public WIFISensor()
        {
            name = "WIFI Sensor";
            wifi_list = new List<string>();
            myTimer = new Timer();
            myTimer.Elapsed += new ElapsedEventHandler(TimerEventArrived);
            myTimer.Interval = TIMER_INTERVAL;
            myTimer.Start();
            populate();
            data = wifi_list;
            if (wifi_list.Count == 0) data = null;
            Notify();
        }

        private void populate()
        {
            try
            {
                ManagementScope managementScope = new ManagementScope("\\\\.\\root\\wmi");
                System.Management.ObjectQuery objectQuery = new
                System.Management.ObjectQuery(wql_network_list);
                ManagementObjectSearcher managementObjectSearcher = new
                ManagementObjectSearcher(managementScope, objectQuery);
                ManagementObjectCollection moc = managementObjectSearcher.Get();
                ManagementObjectCollection.ManagementObjectEnumerator moe =
                moc.GetEnumerator();
                moe.MoveNext();
                ManagementBaseObject[] objarr =
                (ManagementBaseObject[])moe.Current.Properties["Ndis80211BSSIList"].Value;
                if (objarr != null)
                {
                    foreach (ManagementBaseObject obj in objarr)
                    {
                        string x;
                        char[] ssid =
                        Encoding.ASCII.GetChars((byte[])obj["Ndis80211Ssid"]);
                        Console.Write("SSID: ");
                        x = "SSID: ";
                        int i;
                        for (i = 0; i < 32 && ssid[i] != '\0'; i++)
                        {
                            x = x + ssid[i];
                            Console.Write(ssid[i]);
                        }
                        uint rs = (uint)obj["Ndis80211Rssi"];
                        //Console.Write(".");
                        //Console.WriteLine(" RSSI: " + rs);
                        x = x + ".";
                        x = x + " RSSI: " + rs;
                        wifi_list.Add(x);
                    }
                }
            }
            catch (Exception f)
            {

            }
        }
        private void TimerEventArrived(object source, ElapsedEventArgs f)
        {
            wifi_list.Clear();
            populate();
            data = wifi_list;
            if (wifi_list.Count == 0) data = null;
            Notify();
        }
        public void run()
        {
            Console.Clear();
            populate();
        }
    }
}